package com.easylinkin.linkappapi.lobar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.entity.ClockConfig;
import com.easylinkin.linkappapi.lobar.mapper.ClockConfigMapper;
import com.easylinkin.linkappapi.lobar.service.ClockConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 闸机表 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class ClockConfigServiceImpl extends ServiceImpl<ClockConfigMapper, ClockConfig> implements ClockConfigService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param clockConfig
   */
  @Override
  public void insert(ClockConfig clockConfig) {
    clockConfig.setId(null);
    /**
     * 验证重复
     */
//    this.checkExist(clockConfig);
    //设置基本属性
    this.setBase(clockConfig);
    this.save(clockConfig);
  }

  /**
   * 根据id编辑
   * @param clockConfig
   */
  @Override
  public void updateOne(ClockConfig clockConfig) {
    /**
     * 验证重复
     */
//    this.checkExist(clockConfig);
    //设置基本属性
    this.setBase(clockConfig);
    this.updateById(clockConfig);
  }

  /**
   * @Description: 查询当前正在执行的考勤规则
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @Override
  public ClockConfig findNew() {
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    QueryWrapper<ClockConfig> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tenant_id_",tenantId)
        .or().isNull("tenant_id_")
        .orderByDesc("create_time_");
    List<ClockConfig> clockConfigs = baseMapper.selectList(queryWrapper);
    if (clockConfigs.size() == 1){
      return clockConfigs.get(0);
    }else if (clockConfigs.size() > 1){
      ClockConfig config = clockConfigs.stream().filter(c -> tenantId.equals(c.getTenantId()))
          .collect(Collectors.toList()).get(0);
      return config;
    }
    return null;
  }

  @Override
  public IPage<ClockConfig> queryListByPage(RequestModel<ClockConfig> requestModel) {
    Page page = requestModel.getPage();
    ClockConfig clockConfig = requestModel.getCustomQueryParams();
    //项目id 过滤
    clockConfig.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    IPage<ClockConfig> clockConfigIPage = baseMapper.queryListByPage(page, clockConfig);
    return clockConfigIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(ClockConfig clockConfig) {
    QueryWrapper<ClockConfig> queryWrapper = new QueryWrapper<>();
    //todo 设置判断重复条件
    //编辑的时候存在id
    Optional.ofNullable(clockConfig.getId()).ifPresent(id -> queryWrapper.ne("id_",clockConfig.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该考勤规则已存在");
    }
  }

  /**
   * 设置基本属性
   * @param clockConfig
   */
  private void setBase(ClockConfig clockConfig) {
    clockConfig.setModifyTime(new Date());
    clockConfig.setModifyId(linkappUserContextProducer.getCurrent().getId());
    //没有id就是新增,有就是编辑
    if (null == clockConfig.getId()){
      clockConfig.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      clockConfig.setCreateTime(new Date());
      clockConfig.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    }
  }
}
